package com.fwj.user.service.user.rbac;

import com.alibaba.fastjson.JSONObject;
import com.fwj.core.common.common.CommonUtils;
import com.fwj.core.common.constant.DataConstant;
import com.fwj.core.common.model.PageRequest;
import com.fwj.core.common.model.PageResponse;
import com.fwj.core.common.model.ResponseDTO;
import com.fwj.user.api.rbac.RBACMenuService;
import com.fwj.user.dao.rbac.RBACMenuMapper;
import com.fwj.user.entity.rbac.menu.Menu;
import com.fwj.user.model.rbac.menu.MenuVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Author: fwj
 * @Data: 2022/5/1 17:44
 * @Version 1.0
 * @Description:
 */

@Service
public class RBACMenuServiceImpl implements RBACMenuService {

    //添加日志工厂
    private static final Logger LOGGER = LoggerFactory.getLogger(RBACMenuServiceImpl.class);

    @Autowired
    private RBACMenuMapper menuMapper;

    @Override
    @Transactional
    public ResponseDTO saveMenu(Menu menu) {
        try {
            Menu execMenu = buildMenu(menu);
            menuMapper.insertSelective(execMenu);
            return new ResponseDTO().success();
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO().fail();
        }
    }

    private Menu buildMenu(Menu menu) {
        Menu retMenu = new Menu();
        //生成角色编码
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String number = simpleDateFormat.format(timestamp) + CommonUtils.generateUniqueNumber();

        retMenu.setMenuNumber(number);
        retMenu.setTitle(menu.getTitle());
        retMenu.setCreator(menu.getCreator());
        retMenu.setCreateTime(timestamp);
        retMenu.setUpdateTime(timestamp);
        retMenu.setStatus(DataConstant.NORMAL);
        return retMenu;
    }

    @Override
    @Transactional
    public ResponseDTO deleteMenu(String menuNumber) {
        try {
            Example example = new Example(Menu.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("menuNumber",menuNumber);
            menuMapper.deleteByExample(example);
            return new ResponseDTO().success();
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO().fail();
        }
    }

    @Override
    public ResponseDTO queryMenu(JSONObject jsonObject) {
        try {
            PageRequest pageRequest = JSONObject.parseObject(jsonObject.toJSONString(),PageRequest.class);
            PageHelper.startPage(pageRequest.getPage(), pageRequest.getPageSize());
            Example example = buildMenuExample(pageRequest.getRequestBody());
            PageInfo<Menu> pageInfo = new PageInfo<>(menuMapper.selectByExample(example));
            PageResponse<MenuVo> pageResponse = new PageResponse<>();
            pageResponse.setPageTotal(pageInfo.getTotal());
            List<MenuVo> menuVoList = JSONObject.parseArray(JSONObject.toJSONString(pageInfo.getList()),MenuVo.class);
            pageResponse.setResponseBody(menuVoList);
            return new ResponseDTO().success().setData(pageResponse);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO().fail();
        }
    }

    private Example buildMenuExample(JSONObject requestBody) {
        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",DataConstant.NORMAL);
        if (CommonUtils.isEmpty(requestBody)){ return example; }
        MenuVo menuVo = JSONObject.parseObject(requestBody.toJSONString(),MenuVo.class);
        if (CommonUtils.isNotEmpty(menuVo.getMenuNumber())){
            criteria.andLike("roleNumber",menuVo.getMenuNumber());
        }
        if (CommonUtils.isNotEmpty(menuVo.getTitle())){
            criteria.andLike("roleName",menuVo.getTitle());
        }
        return example;
    }
}
